import datetime
from google.appengine.ext import db

class Invoice(db.Model):
	number      = db.StringProperty()
	company     = db.StringProperty()
	comments    = db.TextProperty()
	created     = db.DateTimeProperty(auto_now_add=True)
	startDate   = db.DateTimeProperty()
	endDate     = db.DateTimeProperty()
	hours       = db.FloatProperty()
	rate        = db.FloatProperty()
	taxRate     = db.FloatProperty()
	paid        = db.BooleanProperty()
	sent        = db.BooleanProperty()
	range       = db.ListProperty(datetime.datetime)
	
	@property
	def amount(self):
		return self.hours * self.rate
		
	@property
	def amountGST(self):
		return (self.rate * self.hours) + ((self.rate * self.hours) * self.taxRate)
	@classmethod
	def get_by_number(cls, number):
		return cls.all().filter('number =', number).get()
		
	@classmethod
	def get_range(cls, startDate, endDate):
		result = []
		for n in cls.gql('where range >= :1 and range <= :2 order by range', startDate, endDate):
			if n.startDate >= startDate and n.endDate <= endDate:
				result.append(n)
		return result				